import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';

class Tuya {
  static const MethodChannel _channel = MethodChannel('tuya');

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<String?> get Login async {
    try {
      final res = await _channel.invokeMethod('Login');
      if (Platform.isAndroid) {
        return res;
      } else if (Platform.isIOS) {
        if (res is String && res == "1") {
          return "success";
        } else {
          return null;
        }
      }
    } catch (e) {
      print(e.toString());
    }
  }

  static Future<String?> get getHomeList async {
    try {
      return await _channel.invokeMethod('getHomeList');
    } catch (e) {
      print(e.toString());
    }
  }

  static Future<String?> createHome(String name) async {
    try {
      return await _channel.invokeMethod('createHome', <String, dynamic>{
        'name': name,
      });
    } catch (e) {
      print(e.toString());
    }
  }

  static Future<String?> getHomeDetail(num id) async {
    try {
      return await _channel.invokeMethod('getHomeDetail', <String, dynamic>{
        'id': id,
      });
    } catch (e) {
      print(e.toString());
    }
  }

  static Future<String?> get StartScanDevice async {
    try {
      return await _channel.invokeMethod('StartScanDevice');
    } catch (e) {
      print(e.toString());
    }
  }

  static Future<bool?> get stopScanDevice async {
    try {
      final res = await _channel.invokeMethod('stopScanDevice');
      if (Platform.isAndroid) {
        return res;
      } else if (Platform.isIOS) {
        if (res is String && res == "1") {
          return true;
        } else {
          return false;
        }
      }
    } catch (e) {
      print(e.toString());
    }
  }

  /// 这里需要注意，android 和 iOS 可能存在不同
  static Future<String?> activeDevice(num homeId, String uuid) async {
    try {
      return await _channel.invokeMethod(
          'activeDevice', <String, dynamic>{'homeId': homeId, 'uuid': uuid});
    } catch (e) {
      print(e.toString());
    }
  }

  static Future<bool?> resetFactory(String uuid) async {
    try {
      final res = await _channel.invokeMethod('resetFactory', <String, dynamic>{
        'uuid': uuid,
      });
      if (Platform.isAndroid) {
        return res;
      } else if (Platform.isIOS) {
        if (res is String && res == "1") {
          return true;
        } else {
          return false;
        }
      }
    } catch (e) {
      print(e.toString());
    }
  }

  static Future<bool?> removeDevice(String uuid) async {
    try {
      final res = await _channel.invokeMethod('removeDevice', <String, dynamic>{
        'uuid': uuid,
      });
      if (Platform.isAndroid) {
        return res;
      } else if (Platform.isIOS) {
        if (res is String && res == "1") {
          return true;
        } else {
          return false;
        }
      }
    } catch (e) {
      print(e.toString());
    }
  }

  static Future<String?> LoginById(String uid) async {
    try {
      final res = await _channel.invokeMethod('LoginById', <String, dynamic>{
        'uid': uid,
      });
      if (Platform.isAndroid) {
        return res;
      } else if (Platform.isIOS) {
        if (res is String && res == "1") {
          return "success";
        } else {
          return null;
        }
      }
    } catch (e) {
      print(e.toString());
    }
  }
}
